class AddressesController < ApplicationController
  def new
    @address = Address.new
    @driver = Driver.find(params[:driver_id])
  end

  def create
    @driver = Driver.find(params[:driver_id])
    respond_to do |format|
      if @driver.addresses.create(params[:address])
        format.html  { redirect_to(addresses_driver_path(@driver),
                      :notice => 'Address was added successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end

  def edit
    @address = Address.find(params[:id])
    @driver = Driver.find(params[:driver_id])
  end

  def update
    @driver = Driver.find(params[:driver_id])
    @address = Address.find(params[:id])
    respond_to do |format|
      if @address.update_attributes(params[:address])
        format.html  { redirect_to(addresses_driver_path(@driver),
                      :notice => 'Address was updated successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @driver = Driver.find(params[:driver_id])
    @address = Address.find(params[:id])
    respond_to do |format|
      if Driver.delete_address(params[:driver_id], params[:id])
        format.html  { redirect_to(addresses_driver_path(@driver),
                      :notice => 'Address was deleted successfully for the driver.') }
        format.json  { render :json => @driver,
                      :status => :created, :location => @driver }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @driver.errors,
                      :status => :unprocessable_entity }
      end
    end
  end 
end
